package edu.gatech.angry12.UI.client;

import java.security.MessageDigest;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.Frame;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SeedAppEntry implements EntryPoint {
	
	protected Composite toDisplay;
	
	private SeedAppMenuBar seedAppMenuBar;
	public void onModuleLoad() {
				
		RootPanel rootPanel = RootPanel.get();
		rootPanel.setSize("1024", "768");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		rootPanel.add(verticalPanel, 5, 5);
		verticalPanel.setSize("1024px", "509px");
		
		SeedAppHeader seedAppHeader = new SeedAppHeader();
		verticalPanel.add(seedAppHeader);
		seedAppHeader.setSize("1024", "70");
		
		
		
		
		toDisplay = new Login();	
		
		seedAppMenuBar = new SeedAppMenuBar();
		verticalPanel.add(seedAppMenuBar);
		seedAppMenuBar.setWidth("449px");
		verticalPanel.setCellHorizontalAlignment(seedAppMenuBar, HasHorizontalAlignment.ALIGN_CENTER);
		seedAppMenuBar.setVisible(false);
		
		// Frame frame = new Frame("http://www.google.com");
		verticalPanel.add(toDisplay);
		toDisplay.setHeight("343px");
		verticalPanel.setCellHorizontalAlignment(toDisplay, HasHorizontalAlignment.ALIGN_CENTER);
		// frame.setSize("942px", "372px");
		// frame.add
		
		SeedAppFooter seedAppFooter = new SeedAppFooter();
		verticalPanel.add(seedAppFooter);
				
	}
	
	protected void updateDisplay(Composite content) {
		toDisplay = content;
		
		if (!(content instanceof Login)) {
			seedAppMenuBar.setVisible(true);
		}
	}
	
	/**
	 * hashPassowrd method uses SHA-256 algorithm to hash the password 
	 * 
	 * @param password
	 * @return a String representation of hexed password
	 * @throws Exception for now
	 */
	private String hashPassword(String password) throws Exception {
		
		MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(password.getBytes());
 
        byte byteData[] = md.digest();    
 
        StringBuffer hexString = new StringBuffer();
    	for (int i=0;i<byteData.length;i++) {
    		String hex=Integer.toHexString(0xff & byteData[i]);
   	     	if(hex.length()==1) hexString.append('0');
   	     	hexString.append(hex);
    	}
    	
    	return hexString.toString();
	}
}
